from typing import Optional

import logging
import aiohttp

from .abc import Notice

logger = logging.getLogger("main")

class DreamixDingtalkGroup(Notice):
    def __init__(self, token: str, group_id: str):
        super().__init__(token)
        self._token = token
        self._group_id = group_id

    async def send(self, msg: str, title: Optional[str] = None, importance: Optional[int] = 1) -> None:
        if title is None:
            title = msg[:5]

        try:
            async with aiohttp.ClientSession() as session:
                async with session.get(
                        "https://www.dreamix.top/api/UMS/dingtalk/send_message_group",
                        params={
                            "title": title,
                            "content": msg,
                            "group_id": self._group_id,
                            "importance": importance,
                        },
                        headers={"Authorization": f"Bearer {self._token}"}) as resp:
                    data = await resp.text()
                    logger.debug(data)
                    pass  # TODO(错误处理)
        except Exception as e:
            logger.exception(e)
            return